package com.yzf;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.hzl.Constants;
import com.hzl.ContactsActivity;
import com.hzl.UserInfoBean;
import com.yao.component.circle.R;
import com.yzf.bean.GroupBean;


import java.util.ArrayList;


public class LabelActivity extends Activity {
    private ArrayList<GroupBean> groupBeanList = new ArrayList<GroupBean>();
    private  GroupBean groupBean = new GroupBean();
    private LabelAdapter adapter;
    private int pos = -1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_label);
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LabelActivity.this.finish();
            }
        });
        findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LabelActivity.this, ContactsActivity.class);
                intent.putExtra("type",Constants.NEW_START_LABEL);
                startActivityForResult(intent,1);
            }
        });
        groupBeanList = (ArrayList<GroupBean> )getIntent().getSerializableExtra("groupList");
        //把标签行删除
        groupBeanList.remove(groupBeanList.size()-1);
        ListView listView = (ListView)findViewById(R.id.label_listview);
        adapter = new LabelAdapter();
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                pos = position;
                GroupBean groupBean = groupBeanList.get(position);
                Intent intent = new Intent(LabelActivity.this, LabelMemberActivity.class);
                intent.putExtra("groupBean",groupBean);
                intent.putExtra("type","showLabelMember");
                startActivityForResult(intent,3);
            }
        });
    }
    class LabelAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return groupBeanList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View inflate = getLayoutInflater().inflate(R.layout.label_item, null);
            GroupBean groupBean = groupBeanList.get(position);
            int size = groupBean.getPersonList().size();
            TextView name = (TextView) inflate.findViewById(R.id.label_tv);
            name.setText(groupBean.getName()+"("+size+")");
            return inflate;
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        groupBeanList.clear();
        groupBeanList.addAll(Data.bufenGroupBeanList);
        if(adapter!=null){
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data!=null){
//            if(requestCode == 1&&resultCode == Constants.CODE_FRIEND_LIST) {
            if(requestCode == 1&&resultCode == Constants.CODE_FRIEND_LIST) {
                groupBean.setPersonList((ArrayList<UserInfoBean>) data.getSerializableExtra("UserInfoBean"));
                Intent intent = new Intent(LabelActivity.this, LabelMemberActivity.class);
                intent.putExtra("groupBean",groupBean);
                intent.putExtra("type","addLabel");
                startActivityForResult(intent,2);
            }else if(requestCode == 2  && resultCode == Constants.ADD_LABEL_CODE){//添加标签
                GroupBean groupBean = (GroupBean) data.getSerializableExtra("groupBean");
                groupBean.getPersonList().remove(groupBean.getPersonList().size()-2);//移除加好
                groupBean.getPersonList().remove(groupBean.getPersonList().size()-1);//移除减号
                groupBeanList.add(groupBean);
                //暂时没有数据，先把数据保存到Data下
                adapter.notifyDataSetChanged();
                Data.bufenGroupBeanList.clear();
                Data.bufenGroupBeanList.addAll(groupBeanList);
////                //在最后一行设置标签行
//                Data.bufenGroupBeanList.add(new GroupBean());
            }else if(requestCode == 3   && resultCode == Constants.ADD_MEMBER_CODE){//添加标签成员
                GroupBean groupBean = (GroupBean) data.getSerializableExtra("groupBean");
                groupBeanList.remove(pos);
                groupBeanList.add(pos,groupBean);
                groupBean.getPersonList().remove(groupBean.getPersonList().size()-2);//移除加好
                groupBean.getPersonList().remove(groupBean.getPersonList().size()-1);//移除减号
                //暂时没有数据，先把数据保存到Data下
                adapter.notifyDataSetChanged();
                Data.bufenGroupBeanList.clear();
                Data.bufenGroupBeanList.addAll(groupBeanList);
////                //在最后一行设置标签行
//                Data.bufenGroupBeanList.add(new GroupBean());

            }else if(requestCode == 3   && resultCode == Constants.DEL_MEMBER_CODE){
                String groupId = data.getStringExtra("groupId");
                for(int i = 0;i<groupBeanList.size();i++){
                    GroupBean groupBean = groupBeanList.get(i);
                    if(groupId.equals(groupBean.getId()+"")){
                         groupBeanList.remove(pos);
                    }
                }
                //暂时没有数据，先把数据保存到Data下
                adapter.notifyDataSetChanged();
                Data.bufenGroupBeanList.clear();
                Data.bufenGroupBeanList.addAll(groupBeanList);
////                //在最后一行设置标签行
//                Data.bufenGroupBeanList.add(new GroupBean());

            }
        }
    }
}
